<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()"
             label-width="80px">
      <el-form-item label="申请人ID" prop="userId">
        <el-input v-model="dataForm.userId" placeholder="申请人ID"></el-input>
      </el-form-item>
      <el-form-item label="真实姓名" prop="realName">
        <el-input v-model="dataForm.realName" placeholder="真实姓名"></el-input>
      </el-form-item>
      <el-form-item label="身份证号码" prop="idNum">
        <el-input v-model="dataForm.idNum" placeholder="身份证号码"></el-input>
      </el-form-item>
      <el-form-item label="身份证正面" prop="facade">
        <el-input v-model="dataForm.facade" placeholder="身份证正面"></el-input>
      </el-form-item>
      <el-form-item label="身份证反面" prop="reverse">
        <el-input v-model="dataForm.reverse" placeholder="身份证反面"></el-input>
      </el-form-item>
      <el-form-item label="审核状态：0->已提交;1->已通过;2->已拒绝;" prop="status">
        <el-input v-model="dataForm.status" placeholder="审核状态：0->已提交;1->已通过;2->已拒绝;"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          createTime: '',
          deleted: '',
          version: '',
          lastModifyTime: '',
          userId: '',
          realName: '',
          idNum: '',
          facade: '',
          reverse: '',
          status: ''
        },
        dataRule: {
          userId: [
            {required: true, message: '申请人ID不能为空', trigger: 'blur'}
          ], realName: [
            {required: true, message: '真实姓名不能为空', trigger: 'blur'}
          ], idNum: [
            {required: true, message: '身份证号码不能为空', trigger: 'blur'}
          ], facade: [
            {required: true, message: '身份证正面不能为空', trigger: 'blur'}
          ], reverse: [
            {required: true, message: '身份证反面不能为空', trigger: 'blur'}
          ], status: [
            {required: true, message: '审核状态：0->已提交;1->已通过;2->已拒绝;不能为空', trigger: 'blur'}
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/manage/qualification/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 200) {
                Object.assign(this.dataForm, data.data)
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/manage/qualification/${!this.dataForm.id ? 'save' : 'update'}`),
              method: `${!this.dataForm.id ? 'post' : 'put'}`,
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'userId': this.dataForm.userId,
                'realName': this.dataForm.realName,
                'idNum': this.dataForm.idNum,
                'facade': this.dataForm.facade,
                'reverse': this.dataForm.reverse,
                'status': this.dataForm.status
              })
            }).then(({data}) => {
              if (data && data.code === 200) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
